package com.example.electricbicycle.dao;

import com.example.electricbicycle.po.AfterSalesAdmin;
import utils.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AfterSalesAdminDAOImpl implements AfterSalesAdminDAO {

    @Override
    public List<AfterSalesAdmin> getAllAfterSales() {
        List<AfterSalesAdmin> afterSalesList = new ArrayList<>();
        String sql = "SELECT id, sale_order_id, user_id, problem, status, create_time FROM after_sale_order";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                AfterSalesAdmin afterSales = new AfterSalesAdmin();
                afterSales.setId(rs.getInt("id"));
                afterSales.setSaleOrderId(rs.getInt("sale_order_id"));
                afterSales.setUserId(rs.getInt("user_id"));
                afterSales.setProblem(rs.getString("problem"));
                afterSales.setStatus(rs.getInt("status"));
                afterSales.setCreateTime(rs.getTimestamp("create_time"));
                afterSalesList.add(afterSales);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return afterSalesList;
    }

    @Override
    public AfterSalesAdmin getAfterSalesById(int id) {
        String sql = "SELECT * FROM after_sale_order WHERE id = ?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    AfterSalesAdmin afterSales = new AfterSalesAdmin();
                    afterSales.setId(rs.getInt("id"));
                    afterSales.setSaleOrderId(rs.getInt("sale_order_id"));
                    afterSales.setUserId(rs.getInt("user_id"));
                    afterSales.setProblem(rs.getString("problem"));
                    afterSales.setStatus(rs.getInt("status"));
                    afterSales.setCreateTime(rs.getTimestamp("create_time"));
                    return afterSales;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addAfterSales(AfterSalesAdmin afterSales) {
        String sql = "INSERT INTO after_sale_order (sale_order_id, user_id, problem, status, create_time) VALUES (?, ?, ?, ?, ?)";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, afterSales.getSaleOrderId());
            pstmt.setInt(2, afterSales.getUserId());
            pstmt.setString(3, afterSales.getProblem());
            pstmt.setInt(4, afterSales.getStatus());
            pstmt.setTimestamp(5, new Timestamp(afterSales.getCreateTime().getTime()));

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateAfterSales(AfterSalesAdmin afterSales) {
        String sql = "UPDATE after_sale_order SET sale_order_id=?, user_id=?, problem=?, status=?, create_time=? WHERE id=?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, afterSales.getSaleOrderId());
            pstmt.setInt(2, afterSales.getUserId());
            pstmt.setString(3, afterSales.getProblem());
            pstmt.setInt(4, afterSales.getStatus());
            pstmt.setTimestamp(5, new Timestamp(afterSales.getCreateTime().getTime()));
            pstmt.setInt(6, afterSales.getId());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteAfterSales(int id) {
        String sql = "DELETE FROM after_sale_order WHERE id=?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
